package net1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.UUID;

public class TcpFileServer {
    private static final int port = 8888;
    private static final String path = "";

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(port);
        while (true) {
            Socket socket = server.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("------------------------------");
                        System.out.println("客户端建立TCP连接...");
                        System.out.println("客户端IP: " + socket.getInetAddress().getHostAddress());
                        InputStream inputStream = socket.getInputStream();

                        Request request = Request.serverParse(inputStream);
                        System.out.println("服务器收到请求： " + request);

                        Response response = build(request);
                        OutputStream outputStream = socket.getOutputStream();
                        System.out.println("服务器返回响应： " + response);
                        response.serverWrite(outputStream);
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static Response build(Request request) throws IOException {
        Response response = new Response();
        response.setStatus(200);
        File url = new File(path + request.getUrl());

        if (!url.exists()) {
            response.setStatus(404);
            response.setData("");
            return response;
        }

        try {
            switch (request.getType()) {
                case 1: {
                    File[] childer = url.listFiles();
                    if (childer == null) {
                        response.setData("");
                    } else {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < childer.length; i++) {
                            File child = childer[i];
                            stringBuilder.append(child.getAbsolutePath().substring(path.length()) + "\n");
                        }
                        response.setData(stringBuilder.toString());
                    }
                    break;
                }
                case 2: {
                    url.renameTo(new File(url.getParent() + File.separator + request.getData()));
                    break;
                }
                case 3: {
                    url.delete();
                    break;
                }
                case 4: {
                    FileWriter upload = new FileWriter(url.getAbsolutePath() + File.separator + UUID.randomUUID());
                    upload.write(request.getData());
                    upload.flush();
                    upload.close();
                    break;
                }
                case 5: {
                    String data = new String(Files.readAllBytes(url.toPath()));
                    response.setData(data);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}
